package cc.ufinity.networkdiagnose.widget;

import android.app.Activity;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.TextView;
import cc.ufinity.networkdiagnose.app.GlobalApp;
import cc.ufinity.networkdiagnose.manager.GetAPInfo;
import cc.ufinity.networkdiagnose.manager.Task;
import cc.ufinity.networkdiagnose.manager.TelManager;
import cc.ufinity.networkdiagnose.manager.WlanManager;
import cc.ufinity.networkdiagnose.view.R;

public class TestWidget_GSM extends GeneralTestWidget {

	//private TextView xinzaobiceshi;
	private TextView xinhaoqinagdu;
	private TextView ftpceshi;
	private TextView httpceshi;
	private TextView ping;
    private Button backBtn;
	private TextView titleView;
	private TextView largerFileFTPDownload;
	private AbsoluteLayout relation;

	public Activity ctx;
	private Button oneKey;
	private WlanManager wlanManager;
	private Handler handler;
	private Task task;
	private Test_Task_Widget_GSM ttw_GSM;

	public TestWidget_GSM(Activity context) {
		super(context);
		inflate(context, R.layout.networktest_gsm, this);
		this.ctx = context;	
		titleView = (TextView)findViewById(R.id.titleId_gsm); 
		GlobalApp.isPeapTested = false;
		task = (Task) ctx.getIntent().getSerializableExtra(TAG_REFER_TASK);//获取任务
		titleView.setText("GSM网络");
		ctx.getIntent().putExtra(TAG_REFER_TASK, GlobalApp.taskGSM);
		 
		//xinzaobiceshi = (TextView) findViewById(R.id.xinzaobibutton_gsm);
		largerFileFTPDownload = (TextView) findViewById(R.id.lFile_FTPDowln_wlan);
		xinhaoqinagdu = (TextView) findViewById(R.id.levelbutton_gsm);
		ftpceshi = (TextView) findViewById(R.id.ftpbutton_gsm);
		httpceshi = (TextView) findViewById(R.id.httpbutton_gsm);
		ping = (TextView) findViewById(R.id.pingbutton_gsm);
		relation = (AbsoluteLayout) findViewById(R.id.relationjianmian_gsm);
        backBtn = (Button) findViewById(R.id.test_btnback_gsm);
		oneKey = (Button) this.findViewById(R.id.UPLOADButton_gsm);
        backBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                ctx.setContentView(new SelectNetType_main(ctx));
            }
        });
		oneKey.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					oneKey.setBackgroundResource(R.drawable.yijianceshibeijing);
				} else if (event.getAction() == KeyEvent.ACTION_UP) {
					oneKey.setBackgroundResource(R.drawable.top);
				}
				return false;
			}
		});

		/*xinzaobiceshi.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					relation.setBackgroundResource(R.drawable.gsm_td_xzb);
				}
				return false;
			}
		});
		xinzaobiceshi.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				  doWifiSNRTest();
			}
		});*/
		largerFileFTPDownload.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					relation.setBackgroundResource(R.drawable.gsm_td_xzb);
				}
				return false;
			}
		});

		largerFileFTPDownload.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				doLfileFTPDownload();
			}
		});
		
		xinhaoqinagdu.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					relation.setBackgroundResource(R.drawable.gsm_td_xhqd);
			
				}
				return false;
			}
		});
		xinhaoqinagdu.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 showRssi();
			}
		});
		
		ftpceshi.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					relation.setBackgroundResource(R.drawable.gsm_td_ftp);
					
				}
				return false;
			}
		});
		ftpceshi.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				  doFtpTest();
			}
		});

		httpceshi.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					relation.setBackgroundResource(R.drawable.gsm_td_http);
				}
				return false;
			}
		});
		httpceshi.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				  doHttpTest();
			}
		});
		 
		ping.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == KeyEvent.ACTION_DOWN) {
					relation.setBackgroundResource(R.drawable.gsm_td_ping);
				}
				return false;
			}
		});
		ping.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				  doPing();
			}
		});
		oneKey.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub //获取TD或GSM测试地点
			    TelManager tm = new TelManager(ctx);
		        int lac = tm.getLac();
		        int ci = tm.getCI();
		        System.out.println("-------GetAPInfo(lac,ci).start2()  lac:"+ lac + "ci:" + ci);
		        new GetAPInfo(lac,ci).start2();
                if(GlobalApp.CITY == null || GlobalApp.CITY.length() < 2) {
                	new GetAPInfo(tm.getPhoneNumber(), false).start3();
                }
				GlobalApp.isWlanFirst = false;
				ttw_GSM = new Test_Task_Widget_GSM(ctx);
 	            ctx.setContentView(ttw_GSM);
			 }
		});
	}

	private void doWifiSNRTest() {
		TD_GSM_WifiSnrTestWidget ww = new TD_GSM_WifiSnrTestWidget(ctx);
		ctx.setContentView(ww);
	}
	
	private void doLfileFTPDownload() {
		FtpDownloadWidget fw = new FtpDownloadWidget(ctx);
		ctx.setContentView(fw);
		fw.statFtpTest();
	}

	private void doHttpTest() {
		HttpTestWidget httpTest = new HttpTestWidget(ctx);
		ctx.setContentView(httpTest);
		httpTest.startHttpTest();
	}

	private void doFtpTest() {
		FtpTestWidget ftpTest = new FtpTestWidget(ctx);
		ctx.setContentView(ftpTest);
		ftpTest.statFtpTest();
	}

	private void doPing() {
		PingWidget ping = new PingWidget(ctx);
		ctx.setContentView(ping);
	}

	private void showRssi() {
		TD_GSM_RssiWidget rw = new TD_GSM_RssiWidget(ctx);
		ctx.setContentView(rw);
	}
}
